package com.ruoyi.factorydata.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.factorydata.domain.MaterialCategory;
import com.ruoyi.factorydata.domain.vo.MaterialCategoryTreeVo;
import com.ruoyi.factorydata.domain.vo.ProcessCategoryTreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.factorydata.mapper.ProcessCategoryMapper;
import com.ruoyi.factorydata.domain.ProcessCategory;
import com.ruoyi.factorydata.service.IProcessCategoryService;

/**
 * 工序分类Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-09
 */
@Service
public class ProcessCategoryServiceImpl extends ServiceImpl<ProcessCategoryMapper, ProcessCategory> implements IProcessCategoryService {
    @Autowired
    private ProcessCategoryMapper processCategoryMapper;

    /**
     * 查询工序分类
     *
     * @param id 工序分类主键
     * @return 工序分类
     */
    @Override
    public ProcessCategory selectProcessCategoryById(Long id) {
        return processCategoryMapper.selectProcessCategoryById(id);
    }

    /**
     * 查询工序分类列表
     *
     * @param processCategory 工序分类
     * @return 工序分类
     */
    @Override
    public List<ProcessCategory> selectProcessCategoryList(ProcessCategory processCategory) {
        return processCategoryMapper.selectProcessCategoryList(processCategory);
    }

    /**
     * 新增工序分类
     *
     * @param processCategory 工序分类
     * @return 结果
     */
    @Override
    public int insertProcessCategory(ProcessCategory processCategory) {
                processCategory.setCreateTime(DateUtils.getNowDate());
            return processCategoryMapper.insertProcessCategory(processCategory);
    }

    /**
     * 修改工序分类
     *
     * @param processCategory 工序分类
     * @return 结果
     */
    @Override
    public int updateProcessCategory(ProcessCategory processCategory) {
                processCategory.setUpdateTime(DateUtils.getNowDate());
        return processCategoryMapper.updateProcessCategory(processCategory);
    }

    /**
     * 批量删除工序分类
     *
     * @param ids 需要删除的工序分类主键
     * @return 结果
     */
    @Override
    public int deleteProcessCategoryByIds(Long[] ids) {
        int i = processCategoryMapper.deleteBatchIds(Arrays.stream(ids).collect(Collectors.toList()));
        return i;
    }

    /**
     * 删除工序分类信息
     *
     * @param id 工序分类主键
     * @return 结果
     */
    @Override
    public int deleteProcessCategoryById(Long id) {
        return processCategoryMapper.deleteProcessCategoryById(id);
    }

    @Override
    public List<ProcessCategoryTreeVo> getProcessCategoryTree(ProcessCategory processCategory) {
        List<ProcessCategory> processCategories = processCategoryMapper.selectList(null);
        List<ProcessCategoryTreeVo> processCategoryTreeVoList = new ArrayList<>();
        List<ProcessCategoryTreeVo> processCategoryTreeVos = processCategories.stream().map(item -> {
            return ProcessCategoryTreeVo.builder()
                    .id(item.getId())
                    .name(item.getCategoryName())
                    .parentId(0L)
                    .type("processCategory")
                    .build();
        }).collect(Collectors.toList());
        ProcessCategoryTreeVo processCategoryTreeVo = ProcessCategoryTreeVo.builder()
                .id(0L)
                .name("全部工序")
                .parentId(null)
                .type("root")
                .children(processCategoryTreeVos)
                .build();
        processCategoryTreeVoList.add(processCategoryTreeVo);
        return processCategoryTreeVoList;
    }
}